package com.hilaver.dzmis.exhibition.service;

import java.util.List;

import com.hilaver.dzmis.exhibition.hibernate.ExOrder;
import com.hilaver.dzmis.exhibition.hibernate.ExOrderItem;
import com.hilaver.dzmis.exhibition.utils.Constants;
import com.hilaver.dzmis.exhibition.utils.SimpleObj2XML;


public class OrderService extends AbstractBaseService {
	
	public String delete(int id) throws Exception {
		return super.delete(ExOrder.class.getName(), id);
	}
	
	public String deleteItem(int id) throws Exception {
		return super.delete(ExOrderItem.class.getName(), id);
	}

	
	public String get(int id) throws Exception {
		ExOrder obj = (ExOrder)this.dao.get(ExOrder.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		Double totalPrice = this.dao.exePriceHql("SELECT sum(totalPrice) FROM " + ExOrderItem.class.getName() + " where exOrder.id = " + obj.getId());
		
		xmlSB.append("<payment1>");
		xmlSB.append(totalPrice * 0.3d);
		xmlSB.append("</payment1>");
		
		xmlSB.append("<payment2>");
		xmlSB.append(totalPrice * 0.7d);
		xmlSB.append("</payment2>");
		
		xmlSB.append("<totalPrice>");
		xmlSB.append(totalPrice);
		xmlSB.append("</totalPrice>");
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String edit(ExOrder obj) throws Exception {
		this.dao.saveOrUpdate(obj);
		return "{success: true, id: " + obj.getId() +"}";
	}
	
	public String editItem(ExOrderItem orderItem) throws Exception{
		this.dao.saveOrUpdate(orderItem);
		return "{success: true, id: " + orderItem.getId() +"}";
	}
	
	public String getAllPagination(int page, String sort, String ExOrder)
			throws Exception {
		return super.getAllPagination(ExOrder.class.getName(), page, sort, ExOrder);
	}
	
	public String getAllPagination(int page, String sort, String ExOrder, String[] filters)
			throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<ExOrder> objList = this.dao.getAllPagination(ExOrder.class.getName(), offset,
				Constants.PAGE_SIZE, sort, ExOrder, filters);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (ExOrder obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			Double totalPrice = this.dao.exePriceHql("SELECT sum(totalPrice) FROM " + ExOrderItem.class.getName() + " where exOrder.id = " + obj.getId());
			xmlSB.append("<payment1>");
			xmlSB.append(totalPrice * 0.3d);
			xmlSB.append("</payment1>");
			
			xmlSB.append("<payment2>");
			xmlSB.append(totalPrice * 0.7d);
			xmlSB.append("</payment2>");
			
			xmlSB.append("<totalPrice>");
			xmlSB.append(totalPrice);
			xmlSB.append("</totalPrice>");
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ExOrder.class.getName(), filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getAll(String sort, String ExOrder) throws Exception{
		return super.getAll(ExOrder.class.getName(), sort, ExOrder);
	}
	
	public String getAllItem(Integer orderId) throws Exception{
		String[] filters = new String[1];
		int offset = 0;
		filters[0] = "exOrder.id = " + orderId;
		List<ExOrderItem> objList = this.dao.getAll(ExOrderItem.class.getName(), "technicChartMaterial.technicChart.reference, technicChartMaterial.yarn.name, color, quantity", Constants.ORDER_ASC, filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (ExOrderItem obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<id>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getId());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</id>");
			
			xmlSB.append("<color>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getColor());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</color>");
			
			xmlSB.append("<unitPrice>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getTechnicChartMaterial().getUnitPrice()*obj.getCo());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</unitPrice>");
			
			xmlSB.append("<quantity>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getQuantity());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</quantity>");
			
			xmlSB.append("<totalPrice>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getTotalPrice());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</totalPrice>");
			
			xmlSB.append("<technicChart>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getTechnicChartMaterial().getTechnicChart().getReference());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</technicChart>");
			
			xmlSB.append("<yarn>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getTechnicChartMaterial().getYarn().getName());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</yarn>");
			
			
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);		
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		
		return xmlSB.toString();
	}
	
	public ExOrder getObj(int id) throws Exception {
		return (ExOrder)super.getObj(ExOrder.class.getName(), id);
	}
	
	public String getItem(Integer id)throws Exception {
		ExOrderItem orderItem = (ExOrderItem)this.dao.get(ExOrderItem.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append("<id>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getId());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</id>");
		
		xmlSB.append("<color>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getColor());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</color>");
		
		xmlSB.append("<unitPrice>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getTechnicChartMaterial().getUnitPrice());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</unitPrice>");
		
		xmlSB.append("<coefficient>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getCo());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</coefficient>");
		
		xmlSB.append("<quantity>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getQuantity());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</quantity>");
		
		xmlSB.append("<totalPrice>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getTotalPrice());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</totalPrice>");
		
		xmlSB.append("<photoName>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getTechnicChartMaterial().getTechnicChart().getPhotoName());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photoName>");
		
		xmlSB.append("<tcmId>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getTechnicChartMaterial().getId());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</tcmId>");
		
		xmlSB.append("<technicChartId>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getTechnicChartMaterial().getTechnicChart().getId());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</technicChartId>");
		
		xmlSB.append("<q1>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getTechnicChartMaterial().getQ1() == null?"":orderItem.getTechnicChartMaterial().getQ1());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</q1>");
		
		xmlSB.append("<c1>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getTechnicChartMaterial().getC1() == null?"":orderItem.getTechnicChartMaterial().getC1());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</c1>");
		
		xmlSB.append("<q2>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getTechnicChartMaterial().getQ2() == null?"":orderItem.getTechnicChartMaterial().getQ2());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</q2>");
		
		xmlSB.append("<c2>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getTechnicChartMaterial().getC2() == null?"":orderItem.getTechnicChartMaterial().getC2());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</c2>");
		
		xmlSB.append("<q3>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getTechnicChartMaterial().getQ3() == null?"":orderItem.getTechnicChartMaterial().getQ3());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</q3>");
		
		xmlSB.append("<c3>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getTechnicChartMaterial().getC3() == null?"":orderItem.getTechnicChartMaterial().getC3());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</c3>");
		
		xmlSB.append("<q4>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getTechnicChartMaterial().getQ4() == null?"":orderItem.getTechnicChartMaterial().getQ4());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</q4>");
		
		xmlSB.append("<c4>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getTechnicChartMaterial().getC4() == null?"":orderItem.getTechnicChartMaterial().getC4());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</c4>");
		
		xmlSB.append("<q5>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getTechnicChartMaterial().getQ5() == null?"":orderItem.getTechnicChartMaterial().getQ5());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</q5>");
		
		xmlSB.append("<c5>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(orderItem.getTechnicChartMaterial().getC5() == null?"":orderItem.getTechnicChartMaterial().getC5());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</c5>");
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public static void main(String[] args) throws Exception{
		OrderService orderService = new OrderService();
		orderService.getAllItem(10);
	}


	
}
